package com.netscenery.yang.model;

import com.netscenery.util.ErrTag;


public class Message {
	private ModelElement element;
	private ErrTag severity;
	private String field;
	private String description;
	private ErrorCode code;
	
	public Message(ModelElement element,ErrTag severity, String description){
		if (null == element){
			return;
		}
		
		if (null == severity){
			return;
		}
		
		if (null == description){
			return;
		}
		
		this.element = element;
		this.severity = severity;
		this.description = description;
		return;
	}
	
	public Message(ModelElement element,ErrTag severity, String field,String description){
		if (null == element){
			return;
		}
		
		if (null == severity){
			return;
		}
		
		if (null == description){
			return;
		}
		if (null == field){
			return;
		}
		this.element = element;
		this.severity = severity;
		this.field = field;
		this.description = description;
		return;
	}
	
	public Message(ModelElement element,ErrTag severity, ErrorCode errCode){
		if (null == element){
			return;
		}
		
		if (null == severity){
			return;
		}
		
		if (null == errCode){
			return;
		}
		
		this.element = element;
		this.severity = severity;
		this.code = errCode;
		return;

	}
	public Message(ModelElement element,ErrTag severity, String field,ErrorCode errCode){
		if (null == element){
			return;
		}
		
		if (null == severity){
			return;
		}
		if (null == field){
			return;
		}
		if (null == errCode){
			return;
		}
		
		this.element = element;
		this.severity = severity;
		this.field = field;
		this.code = errCode;
		return;

	}
	/**
	 * @return the element
	 */
	public ModelElement getElement() {
		return element;
	}

	public String getElementName(ModelElement element){
		String elementName= null;
		
		if (element instanceof Type){
			elementName = getElementName(element.getEnv().getParent())
			                            + "/[type:"+element.getName()+"]" ;
		}
		else if (element instanceof Node){
			Node node = (Node)element;
			elementName = "[node:" + node.getPath() + "]";
		}
		else if (element instanceof Uses){
			Uses node = (Uses)element;
			elementName = "[uses:" + node.getPath() + "]";
		}
		else if (element instanceof Import){
			elementName = "[import:" + element.getName() + "]";
		}
		else if (element instanceof Include){
			elementName = "[include:" + element.getName() + "]";
		}
		else if(element instanceof Revision){
			elementName = "[revision:" + element.getName() + "]";
		}
		else if(element instanceof Feature){
			elementName = "[feature:" + element.getName() + "]";
		}
		else if(element instanceof Extension){
			elementName = "[extension:" + element.getName() + "]";
		}
		else if(element instanceof Typedef){
			elementName = "[typedef:" + element.getName() + "]";
		}
		else if(element instanceof YangUnknown){
			elementName = "[unknown:" + element.getName() + "]";
		}
		else if (element instanceof Module){
			elementName = "";
		}
		else if (element instanceof BelongsTo){
			elementName = "[belongsto:"+element.getName()+"]";
		}
		else if (element instanceof Bit){
			elementName = getElementName(element.getEnv().getParent())
			                             + "/[bit:"+element.getName()+"]";
		}
		else if (element instanceof Enum){
			elementName = getElementName(element.getEnv().getParent())
			                             + "/[enum:"+element.getName()+"]";
		}
		else if (element instanceof Pattern){
			elementName = getElementName(element.getEnv().getParent())
			                             + "/[pattern:"+element.getName()+"]";
		}
		else if (element instanceof Range){
			String rangeName= null;
			if (element.getEnv().getParent() instanceof YangString){
				rangeName = "length";
			}
			else {
				rangeName = "range";
			}
			elementName = getElementName(element.getEnv().getParent())
			                             + "/["+rangeName+":"+element.getName()+"]";
		}
		else {
			
		}
		if (null == elementName){
			if (element.getName() != null && 0 != element.getName().length()){
				elementName = "[element:" + element.getName() + "]";
			}
		}
		return elementName;
	}
	public String toString(){
		if (null == element){
			return null;
		}
		if (null == severity){
			return null;
		}
		String elementString = getElementName(element);
		if (null == description && null == code){
			return null;
		}
		else if (null != description && null != code){
			return null;
		}
		else if (null != description){
			return severity.getFieldName() + ": " 
					+ ((null == elementString)?"":elementString) 
					+ ((null == field)?"":("Field:" + field + " "))
					+ description;
		}
		else {
			return severity.getFieldName() + ": " 
					+ ((null == elementString)?"":elementString) 
					+ ((null == field)?"":("Field:" + field + " "))
					+ code.getFieldName();
		}
		
	}

	public ErrTag getSeverity() {
		return severity;
	}
	
	
}
